using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;

namespace Siderys.Blog.CustomField
{
    public partial class UniqueFieldColumn : BaseFieldControl, ICallbackEventHandler
    {
        protected TextBox txtValueField;
        protected RequiredFieldValidator rfvTxtValueField;
        private bool mRequired = false;
        private SPList mListField = null;
        private string mInternalName = string.Empty;
        private string mReturnCheckFieldCallback = string.Empty;

        public UniqueFieldColumn(bool pRequired, string pInternalName, SPList pListField)
        {
            mRequired = pRequired;
            mInternalName = pInternalName;
            mListField = pListField;
        }

        protected override string DefaultTemplateName
        {
            get
            {
                return "txtUniqueColumn";
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return txtValueField.Text;
            }
            set
            {
                EnsureChildControls();
                txtValueField.Text = value.ToString();
            }
        }

        public override void Focus()
        {
            EnsureChildControls();
            txtValueField.Focus();
        }

        protected override void CreateChildControls()
        {
            if (Field == null)
            {
                return;
            }
            base.CreateChildControls();
            if (ControlMode.Equals(SPControlMode.Display))
            {
                return;
            }

            txtValueField = (TextBox)TemplateContainer.FindControl("txtValueField");
            txtValueField.Attributes.Add("onchange", "OnCallbackCheckField(this.value)");
            rfvTxtValueField = (RequiredFieldValidator)TemplateContainer.FindControl("rfvTxtValueField");

            ClientScriptManager cm = Page.ClientScript;
            string cbReference = cm.GetCallbackEventReference(this, "arg", "OnCallbackComplete", "");
            string callbackScript = "function CheckUniqeField(arg, context) {" + cbReference + "; }";
            cm.RegisterClientScriptBlock(this.GetType(), "CheckUniqeField", callbackScript, true);

            if (txtValueField == null)
            {
                throw new Exception("El campo es nulo");
            }

            if (ControlMode.Equals(SPControlMode.New))
            {
                txtValueField.Text = string.Empty;
                rfvTxtValueField.Enabled = mRequired;
                
            }
        }

        public string GetCallbackResult()
        {
            return mReturnCheckFieldCallback;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            CheckUniqueField(eventArgument);
        }

        private void CheckUniqueField(string pValueToCheck)
        {
            string lQueryString = "<Where><Eq><FieldRef Name='" + mInternalName + "' /><Value Type='Text'>" + pValueToCheck + "</Value></Eq></Where>";
            SPQuery lQuery = new SPQuery();
            lQuery.Query = lQueryString;
            SPListItemCollection lResultQuery = mListField.GetItems(lQuery);
            if (lResultQuery.Count > 0)
            {
                mReturnCheckFieldCallback = "El valor: " + pValueToCheck + " ya existe en la lista: " + mListField.Title;
            }
        }

    }
}